package com.start.common.dataRule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.start.common.tools.SpringContextTools;

import java.util.Map;

/**
 * 数据规则构造器
 */
public class DataQueryConstructor {
    /**
     * 实体查询条件处理
     *
     * @param object
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> createEntity(T object) {
        return createEntity(object, true);
    }

    /**
     * 实体查询条件处理
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> createEntity(Class<T> clazz) {
        return createEntity(clazz, true);
    }

    /**
     * 实体查询条件处理
     *
     * @param clazz
     * @param autoHandlerQueryCondition
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> createEntity(Class<T> clazz, boolean autoHandlerQueryCondition) {
        return createEntity(clazz, autoHandlerQueryCondition, true);
    }

    /**
     * 实体查询
     *
     * @param clazz
     * @param autoHandlerQueryCondition
     * @param autHandlerOrder
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> createEntity(Class<T> clazz, boolean autoHandlerQueryCondition, boolean autHandlerOrder) {
        EntityQuery<T> query = new EntityQuery<>(clazz);
        query.analysisDataPermission();
        if (autoHandlerQueryCondition) {
            query.analysisSearchQuery();
        }
        if (autHandlerOrder) {
            query.analysisQueryOrder();
        }
        return query;
    }

    /**
     * 实体查询条件处理
     *
     * @param object
     * @param autoHandlerQueryCondition
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> createEntity(T object, boolean autoHandlerQueryCondition) {
        EntityQuery<T> query = new EntityQuery<>(object);
        query.analysisDataPermission();
        if (autoHandlerQueryCondition) {
            query.analysisSearchQuery();
        }
        query.analysisQueryOrder();
        return query;
    }

    /**
     * 动态表单查询条件处理
     *
     * @param autoHandlerQueryCondition
     * @return
     */
    public static StartQueryWrapper createStartQueryWrapper(boolean autoHandlerQueryCondition) {
        return createStartQueryWrapper(SpringContextTools.getHttpServletRequest().getParameterMap(), autoHandlerQueryCondition);
    }


    /**
     * 动态表单查询条件处理
     *
     * @param
     * @return
     */
    public static StartQueryWrapper createStartQueryWrapper() {
        return createStartQueryWrapper(true);
    }

    /**
     * 动态表单查询条件处理
     *
     * @param autoHandlerQueryCondition
     * @return
     */
    public static StartQueryWrapper createStartQueryWrapper(Map<String, String[]> data, boolean autoHandlerQueryCondition) {
        return createStartQueryWrapper(data,autoHandlerQueryCondition,true);
    }

    /**
     * 动态表单查询条件处理
     *
     * @param autoHandlerQueryCondition
     * @return
     */
    public static StartQueryWrapper createStartQueryWrapper(Map<String, String[]> data, boolean autoHandlerQueryCondition, boolean autoHandlerOrder) {
        StartQueryWrapper query = new StartQueryWrapper(data);
        query.analysisDataPermission();
        if (autoHandlerQueryCondition) {
            query.analysisSearchQuery();
        }
        if (autoHandlerOrder) {
            query.analysisQueryOrder();
        }
        return query;
    }
}
